<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线路标识" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择线路标识" clearable>
          <el-option
            v-for="dict in line_info"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="主机id" prop="hostId">
        <el-select v-model="queryParams.hostId" placeholder="请选择主机id" clearable>
          <el-option
            v-for="dict in host_info"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="光纤编号" prop="fiberNum">
        <el-input
          v-model="queryParams.fiberNum"
          placeholder="请输入光纤通道编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="光纤名称" prop="fiberName">
        <el-input
          v-model="queryParams.fiberName"
          placeholder="请输入光纤通道名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="是否有效" prop="valid">
        <el-select v-model="queryParams.valid" placeholder="请选择是否有效" clearable>
          <el-option
            v-for="dict in valid"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['meas:fiberInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['meas:fiberInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['meas:fiberInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['meas:fiberInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fiberInfoList" @selection-change="handleSelectionChange"
              stripe scrollbar-always-on >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="线路标识" align="center" prop="lineId" width="110">
        <template #default="scope">
          <dict-tag :options="line_info" :value="scope.row.lineId"/>
        </template>
      </el-table-column>
      <el-table-column label="主机id" align="center" prop="hostId"  width="90">
        <template #default="scope">
          <dict-tag :options="host_info" :value="scope.row.hostId"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum"  width="120"/>
      <el-table-column label="光纤通道名称" align="center" prop="fiberName"  width="120"/>
      <el-table-column label="传感器id编号" align="center" prop="sensorId"  width="110"/>
      <el-table-column label="传感器名称" align="center" prop="sensorName"  width="120"/>
      <el-table-column label="传感器类型" align="center" prop="sensorType"  width="100">
        <template #default="scope">
          <dict-tag :options="sensor_type" :value="scope.row.sensorType"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤长度(m)" align="center" prop="fiberLength"  width="100"/>
      <el-table-column label="转折点所在距离" align="center" prop="turnPointDistance"  width="120"/>
      <el-table-column label="光纤型号" align="center" prop="channelModel" v-if="false"/>
      <el-table-column label="检测点间隔(m)" align="center" prop="pointInterval"  width="110"/>
      <el-table-column label="是否有效" align="center" prop="valid">
        <template #default="scope">
          <dict-tag :options="valid" :value="scope.row.valid"/>
        </template>
      </el-table-column>
      <el-table-column label="系数比例" align="center" prop="ratio" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['meas:fiberInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['meas:fiberInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改光纤通道信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="fiberInfoRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="线路标识" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路标识">
            <el-option
              v-for="dict in line_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主机id" prop="hostId">
          <el-select v-model="form.hostId" placeholder="请选择主机id">
            <el-option
              v-for="dict in host_info"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤通道编号" prop="fiberNum">
          <el-input v-model="form.fiberNum" placeholder="请输入光纤通道编号" />
        </el-form-item>
        <el-form-item label="光纤通道名称" prop="fiberName">
          <el-input v-model="form.fiberName" placeholder="请输入光纤通道名称" />
        </el-form-item>
        <el-form-item label="传感器id编号" prop="sensorId">
          <el-input v-model="form.sensorId" placeholder="请输入传感器id编号" />
        </el-form-item>
        <el-form-item label="传感器名称" prop="sensorName">
          <el-input v-model="form.sensorName" placeholder="请输入传感器名称" />
        </el-form-item>
        <el-form-item label="传感器类型" prop="sensorType">
          <el-select v-model="form.sensorType" placeholder="请选择传感器类型">
            <el-option
              v-for="dict in sensor_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤长度(m)" prop="fiberLength">
          <el-input v-model="form.fiberLength" placeholder="请输入光纤长度(m)" />
        </el-form-item>
        <el-form-item label="转折点所在距离" prop="turnPointDistance">
          <el-input v-model="form.turnPointDistance" placeholder="请输入转折点所在距离" />
        </el-form-item>
        <el-form-item label="光纤型号" prop="channelModel">
          <el-input v-model="form.channelModel" placeholder="请输入光纤型号" />
        </el-form-item>
        <el-form-item label="检测点间隔(m)" prop="pointInterval">
          <el-input v-model="form.pointInterval" placeholder="请输入检测点间隔(m)" />
        </el-form-item>
        <el-form-item label="是否有效" prop="valid">
          <el-select v-model="form.valid" placeholder="请选择是否有效">
            <el-option
              v-for="dict in valid"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系数比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入系数比例" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FiberInfo">
import { listFiberInfo, getFiberInfo, delFiberInfo, addFiberInfo, updateFiberInfo } from "@/api/meas/fiberInfo";

const { proxy } = getCurrentInstance();
const { host_info, sensor_type, line_info, valid } = proxy.useDict('host_info', 'sensor_type', 'line_info', 'valid');

const fiberInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lineId: null,
    hostId: null,
    fiberNum: null,
    fiberName: null,
    valid: null,
  },
  rules: {
    lineId: [
      { required: true, message: "线路标识不能为空", trigger: "change" }
    ],
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "blur" }
    ],
    sensorId: [
      { required: true, message: "传感器id编号不能为空", trigger: "blur" }
    ],
    sensorName: [
      { required: true, message: "传感器名称不能为空", trigger: "blur" }
    ],
    fiberLength: [
      { required: true, message: "光纤长度(m)不能为空", trigger: "blur" }
    ],
    turnPointDistance: [
      { required: true, message: "转折点所在距离不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询光纤通道信息列表 */
function getList() {
  loading.value = true;
  listFiberInfo(queryParams.value).then(response => {
    fiberInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    lineId: null,
    hostId: null,
    fiberNum: null,
    fiberName: null,
    sensorId: null,
    sensorName: null,
    sensorType: null,
    fiberLength: null,
    turnPointDistance: null,
    channelModel: null,
    pointInterval: null,
    valid: null,
    ratio: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("fiberInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加光纤通道信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFiberInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改光纤通道信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fiberInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFiberInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFiberInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除光纤通道信息编号为"' + _ids + '"的数据项？').then(function() {
    return delFiberInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('meas/fiberInfo/export', {
    ...queryParams.value
  }, `fiberInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
